/**
#Arsalan Ahmed
#Robert Hall
#Micheal Longchamps

 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class VulnerableServer {
		final static int PORT = 4444;
		static String dbName = "database.sqlite";
		static Connection conn;
	    public static void main(String[] args) throws IOException, SQLException {

	    	System.out.println("Server started.");
	    	 try {
				Class.forName("org.sqlite.JDBC");
			} catch (ClassNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
	    	 
			conn = DriverManager.getConnection("jdbc:sqlite:" + dbName);
				    	
	        ServerSocket serverSocket = null;
	        try {
	            serverSocket = new ServerSocket(PORT);
	        } catch (IOException e) {
	            System.err.println("Could not listen on port: 4444.");
	            System.exit(1);
	        }

	        Socket clientSocket = null;
	        try {
	            clientSocket = serverSocket.accept();
	            System.out.println("Connection established!");
	        } catch (IOException e) {
	            System.err.println("Accept failed.");
	            System.exit(1);
	        }

	        PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
	        BufferedReader in = new BufferedReader(
					new InputStreamReader(
					clientSocket.getInputStream()));
	        String inputLine, outputLine;
	    
	        while ((inputLine = in.readLine()) != null) {
	        	 processInput(inputLine, out);
	        	 out.flush();
	        }
	        out.close();
	        in.close();
	        clientSocket.close();
	        serverSocket.close();
	    }

	    public static void processInput(String str, PrintWriter out) throws SQLException
	    {
	    	Scanner message = new Scanner(str);
	    	message.useDelimiter(":");
	    	String username = message.next();
   	
	    	String password = message.next();
	     	System.out.println("INPUT: " +  username + ":" + password);
	    	Statement stmnt = conn.createStatement();
			
	    	// /**
	    	// VULNERABLE
	    	 String SQL = "select * from Users where name='" + username + "' AND password='" + password + "';";
	    	 ResultSet rs = stmnt.executeQuery(SQL);
	    	// */
	    	
	    	/**
	    	// PATCHED
	    	String SQLa = "select * from Users where name=? AND password=?;";
		    PreparedStatement SQL = conn.prepareStatement(SQLa);
		    SQL.setString(1, username);
		    SQL.setString(2, password);
		    ResultSet rs = SQL.executeQuery();
		    */
	    	
		    System.out.println("SQL Statement: " + SQL.toString());
	    	
		    
		   
		    while (rs.next()) {
		    
		    String result = "";
		      result += "name = " + rs.getString("name") + " email = " + rs.getString("email") + " social = " + rs.getString("social");
		      out.println(result);
		    }
		    rs.close();
	    	
		
	       
	    }
	}


